package com.aizhixin.lab.project.template.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.project.template.entity.ImportTemplate;
import com.aizhixin.lab.project.template.entity.Language;
import com.aizhixin.lab.project.template.repository.ImportTemplateRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@RequestMapping("/api/v1/project/import/template")
@RestController
@Api(tags = "导入模板", value = "导入模板")
public class ImportTemplateController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private ImportTemplateRepository importTemplateRepository;
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "保存", httpMethod = "POST", notes = "保存/编辑<br><br><b>lwq</b>")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token, @RequestBody ImportTemplate importTemplate) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
            importTemplateRepository.save(importTemplate);
            result.put(ApiReturnConstants.SUCCESS, true);

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/getList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取所有模板", httpMethod = "GET", notes = "获取所有模板<br><br><b>lwq</b>")
    public ResponseEntity<?> getList(@RequestHeader("Authorization") String token) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<ImportTemplate> importTemplateList = importTemplateRepository.findAll();
        result.put(ApiReturnConstants.SUCCESS, true);
        result.put(ApiReturnConstants.DATA, importTemplateList);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/delete", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除模板", httpMethod = "GET", notes = "删除模板<br><br><b>lwq</b>")
    public ResponseEntity<?> delete(@RequestHeader("Authorization") String token, @RequestParam Integer id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }

        importTemplateRepository.delete(id);
        result.put(ApiReturnConstants.SUCCESS, true);
        return new ResponseEntity(result, HttpStatus.OK);
    }
}
